#pragma once

#include <iostream>
#include <string>
#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include "Log.hpp"

namespace ThreadModlue
{
    using namespace LogModule;
    static uint32_t number = 1; // 不是原子型的，先不处理
    class Thread
    {
        using  func_t = std::function<void()>; 
    private:
        void Enabledetach()
        {
            LOG(LogLevel::DEBUG) << "线程被分离了";
            _isdetach = true;
        }

        void EnableRunning()
        {
            _isrunning = true;
        }

        // 新线程执行
        static void* Routine(void* args) // 属于类内的成员函数，默认包含this指针！
        {
            Thread* self = static_cast<Thread*>(args);
            self->EnableRunning(); // 修改运行标志位
            if(self->_isdetach)
            {
                self->Detach();
            } 
            pthread_setname_np(self->_tid, self->_name.c_str());
            self->_func(); // 回调处理
            return nullptr;
        }
    public:
        Thread(func_t func)
            : _tid(0), _isdetach(false), _isrunning(false), _ret(nullptr), _func(func)
        {
            _name = "thread-" + std::to_string(number++);
        }

        void Detach()
        {
            if (_isdetach)
                return;
            if (_isrunning)
                pthread_detach(_tid);
            Enabledetach();
        }

        bool Start()
        {
            if (_isrunning)
                return false;
            int n = pthread_create(&_tid, nullptr, Routine, this); // 传this指针
            if (n != 0)
            {
                LOG(LogLevel::ERROR) << "create thread error : " << strerror(n);
                return false;
            }
            else
            {
                LOG(LogLevel::DEBUG) << "create thread success";
                return true;
            }
        }

        bool Stop()
        {
            if (_isrunning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    LOG(LogLevel::ERROR) << "cancel thread error : " << strerror(n);
                    return false;
                }
                else
                {
                    LOG(LogLevel::DEBUG) << _name << " stop!";
                    return true;
                }
            }
            return false;
        }

        void Join()
        {
            if(_isdetach)
            {
                LOG(LogLevel::DEBUG) << "你的线程已经被分离了, 不能join";
            }
            int n = pthread_join(_tid, &_ret);
            if(n != 0)
            {
                LOG(LogLevel::ERROR) << "pthread_join error : " << strerror(n);
                return;
            }
            else
            {
                LOG(LogLevel::DEBUG) << "join success";
            }
        }

        std::string Name()
        {
            return _name;
        }

        ~Thread() {}

    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach;  // 分离标志位
        bool _isrunning; // 运行标志位
        void* _ret;
        func_t _func;
    };
}